package com.itcode.controller;

import com.itcode.pojo.Article;
import com.itcode.pojo.PageBean;
import com.itcode.pojo.Result;
import com.itcode.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @PostMapping
    public Result add(@RequestBody @Validated Article article){
        articleService.add(article);
        return Result.success();
    }

    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId, //参数非必须
            @RequestParam(required = false) String state
    ){
        PageBean<Article> pb = articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(pb);
    }


    /**
     * 获取文章详情
     * @param id 分类id
     * @return 分类详情
     */
    @GetMapping("/detail")
    public Result<Article> detail(Integer id){

        Article article = articleService.findById(id);
        return Result.success(article);
    }

    /**
     * 更新
     * @param article 文章
     * @return
     */
    @PutMapping
    public Result update(@RequestBody @Validated(Article.Update.class) Article article){
        articleService.update(article);
        return Result.success();
    }

    /**
     * 删除
     * @param id 文章id
     * @return 无
     */
    @DeleteMapping
    public Result remove(Integer id){
        articleService.remove(id);
        return Result.success();
    }

}
